package com.findme.fecedetection.ui.fragment

import android.support.v4.view.ViewPager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.findme.fecedetection.R
import com.findme.fecedetection.adapter.HomeAdapter
import com.findme.fecedetection.base.LazyFragment
import com.findme.fecedetection.bean.PetListBean
import com.findme.fecedetection.custom.FrescoImageLoader
import com.findme.fecedetection.net.HttpManager
import com.findme.fecedetection.ui.activity.AddInformationActivity
import com.tanguy.user.utlis.extend.click
import com.tanguy.user.utlis.extend.requestSucceed

import com.youth.banner.Banner
import com.youth.banner.BannerConfig
import org.jetbrains.anko.find
import org.jetbrains.anko.support.v4.startActivity

/**
 * Created by dab on 2017/8/19 0019 15:54
 */

class HomeFragment : LazyFragment() {
    override fun viewLayoutID(): Int = R.layout.fragment_home
    private val mBanner by lazy { view!!.find<Banner>(R.id.banner) }
    private val linearLayout by lazy { view!!.find<LinearLayout>(R.id.layout_indicator) }
    private var data = ArrayList<PetListBean>()
    private val homeAdapter = HomeAdapter(data)


    override fun onFirstVisibleToUser(view: View?) {
        if (view == null) {
            return
        }
        view.apply {
            find<RecyclerView>(R.id.rv_content).apply {
                layoutManager = LinearLayoutManager(activity)
                val homeAdapter = homeAdapter
                adapter = homeAdapter
            }
            click(R.id.tv_found) {
                startActivity<AddInformationActivity>()
            }
        }
        HttpManager.getPage()
                .requestSucceed(this) {
                    homeAdapter.setNewData(it.list)
                }

        homeAdapter.setOnItemClick { _, position ->
            //            activity.startActivity<PatDetailsActivity>()
        }
        initBanner()
    }

    private fun initBanner() {
        mBanner.apply {
            setBannerStyle(BannerConfig.NOT_INDICATOR)
            setImageLoader(FrescoImageLoader())
            setDelayTime(5000)

        }
        val viewPager = mBanner.find<ViewPager>(com.youth.banner.R.id.bannerViewPager)
        viewPager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                upIndicator(position, linearLayout)
            }
        })
        val integers = ArrayList<Int>()
        integers.add(R.mipmap.ic_launcher)
        integers.add(R.mipmap.ic_launcher)
        integers.add(R.mipmap.ic_launcher)
        for (i in integers.indices) {
            LayoutInflater.from(activity).inflate(R.layout.view_banner_indicator_trip, linearLayout)
        }
        mBanner.setImages(integers)
        mBanner.start()
    }

    /**
     * 修改指示器的样式
     */
    private fun upIndicator(position: Int, linearLayout: LinearLayout?) {
        if (linearLayout == null) return
        val pos = position % linearLayout.childCount
        for (i in 0 until linearLayout.childCount) {
            linearLayout.getChildAt(i).findViewById<View>(R.id.view_select).visibility = if (pos == i) View.VISIBLE else View.GONE
            linearLayout.getChildAt(i).findViewById<View>(R.id.view_no_select).visibility = if (pos == i) View.GONE else View.VISIBLE

        }

    }


//    @OnClick(R.id.tv_adopt, R.id.tv_found)
//    fun onViewClicked(view: View) {
//        when (view.id) {
//            R.id.tv_adopt -> Toast.makeText(activity, "喂养宠物", Toast.LENGTH_SHORT).show()
//            R.id.tv_found -> startActivity(Intent(activity, AddInformationActivity::class.java))
//        }
//    }


}
